package uk.co.itmentor.pmff.element;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.Validate;

import uk.co.itmentor.pmff.handler.FFDocumentHandler;
import uk.co.itmentor.pmff.parser.FFSectionIterator;
/**
 * Defines the flat file document which constains one or many sections.  
 * @author mdylag
 *
 */
public class FFDocument {
	
	/** Map of {@link FFSection} by section name*/
	private Map<String,FFSection> sections = new HashMap<String,FFSection>();
	/** Map of {@link FFSection} by the field names*/
	private Map<String,FFSection> sectionsByField = new HashMap<String,FFSection>();
	/** Reference to the flat file*/
	private File file;
	/** Line delimiter*/
	private String lineDelimeter = "\t";
	/** */
	private List<FFDocumentHandler> handlers = new ArrayList<FFDocumentHandler>();
	/**
	 * Protected default costructor to protect create istace.
	 */
	protected FFDocument()
	{
		
	}
	
	/**
	 * Add the new section to the document
	 */
	public void addSection(final FFSection section)
	{
		Validate.notNull(section,"Section is empty.");
		Set<String> fieldNames= section.getFields().keySet();

		sections.put(section.getName(), section);		
		for (String name : fieldNames) {
			sectionsByField.put(name, section);
		}
	}
	
	public FFSection getSection(String key)
	{
		return this.sections.get(key);
	}
	
	public final Map<String,FFSection> getSections()
	{
		return this.sections;
	}
	
	/**
	 * Set the flat file for a document.
	 * @param file
	 */
	public void setFile(File file)
	{
		this.file = file;
	}
	/**
	 * Return the {@link FFSection} or null by field name.
	 * @param name
	 * @return
	 */
	public FFSection getSectionByFieldName(String name)
	{
		Validate.notEmpty("Name of the field is empty.",name);
		return this.sectionsByField.get(name);
	}
	

	public File getFile()
	{
		return this.file;
	}
	
	public void addDocumentHandler(FFDocumentHandler handler)
	{
		
	}
	
	

}
